package cn.edu.xjtu.work.removeNthFromEnd;

/**
 * 删除链表的倒数第N个节点
 * 
 * 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
 */
public class Solution {
  public static void main(String[] args) {

  }

  public ListNode removeNthFromEnd(ListNode head, int n) {
    if (head == null) {
      return null;
    }
    ListNode temp = new ListNode(0, head);
    ListNode left = temp;
    ListNode right = temp;
    for (int i = 0; i < n; i++) {
      right = right.next;
    }
    while (right.next != null) {
      right = right.next;
      left = left.next;
    }
    left.next = left.next.next;
    return temp.next;
  }
}
